// pages/doorlist/doorlist.js
import { doorserverList,createOrder } from '../../api/service'
import { delayPay } from '../../api/order'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    params: {
      pageNum: 1,
      pageSize: 20,
      orderState: '',//wait_pay=待付款，wait_use=待使用，used=已使用
    },
    list: [],
    lastPage: false,
    index: 0,
    origin: -1,
    url: ''
  },
  listpay(e) {
    const index  = e.currentTarget.dataset.index
    delayPay({orderId:this.data.list[index].id}).then(payres => {
      const paydata = JSON.parse(payres.data.expend.pay_info)
      wx.requestPayment({
        timeStamp: paydata.timeStamp,
        nonceStr: paydata.nonceStr,
        package: paydata.package,
        signType: paydata.signType,
        paySign: paydata.paySign,
        success() {
          wx.navigateTo({
            url: '/pages/doortodoor/success/success',
          })
        }
      })
    })
    // const payInfo = this.data.list[index].storeDoorserverProduct
    // const data = {
    //   classifyId: payInfo.classifyId,
    //   classifyCid: payInfo.classifyCid,
    //   grade: payInfo.grade,
    //   price: payInfo.price,
    //   appointmentTime: payInfo.appointmentTime,
    //   severalWeeks: payInfo.severalWeeks,
    //   timeFrame: payInfo.timeFrame,
    //   province: payInfo.province,
    //   provinceId: payInfo.provinceId,
    //   city: payInfo.city,
    //   cityId: payInfo.cityId,
    //   area: payInfo.area,
    //   areaId: payInfo.areaId,
    //   address: payInfo.address,
    //   contacts: payInfo.contacts,
    //   mobile: payInfo.mobile,
    //   imageList: payInfo.imageList,
    //   remark: payInfo.remark
    // }
    // createOrder(data).then(res => {
    //   const orderId = res.data.id
    //   delayPay({orderId}).then(payres => {
    //     const paydata = JSON.parse(payres.data.expend.pay_info)
    //     wx.requestPayment({
    //       timeStamp: paydata.timeStamp,
    //       nonceStr: paydata.nonceStr,
    //       package: paydata.package,
    //       signType: paydata.signType,
    //       paySign: paydata.paySign,
    //       success() {
    //         wx.navigateTo({
    //           url: '/pages/doortodoor/success/success',
    //         })
    //       }
    //     })
    //   })
    // })
  },
  switchTab(e) {
    const index = e.currentTarget.dataset.index
    const state = index == 0 ? '' : index == 1 ? 'wait_pay' : index == 2 ? 'wait_use' : 'used'
    // const params = {
    //   pageNum: 1,
    //   pageSize: 20,
    //   orderState: state
    // }
    this.setData({
      index,
      list: [],
      // params,
      "params.pageNum": 1,
      "params.orderState": state
    })
    this.getList()
  },
  lookmore(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/doordetail/doordetail?id='+id,
    })
  },
  getList() {
    const that = this
    const data = this.data.params
    doorserverList(data).then(res => {
      console.log(that.data.list)
      const list = that.data.list
      that.setData( {
        list: list.concat(res.data.rows),
        "params.pageNum": ++that.data.params.pageNum,
        lastPage: res.data.lastPage
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if(options.origin) {
      if(options.origin == 1) {
        console.log(1)
        this.setData({
          origin: 1,
          url: '/pages/UCenter/index/index'
        })
      }
    }
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.lastPage) {
      return
    } else {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})